package com.lnint.jess.generator.service;


import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lnint.jess.common.persistence.Page;
import com.lnint.jess.common.service.BaseService;
import com.lnint.jess.common.utils.StringUtils;
import com.lnint.jess.generator.dao.GenSceneDao;
import com.lnint.jess.generator.dao.GenSchemeDao;
import com.lnint.jess.generator.entity.GenConfig;
import com.lnint.jess.generator.entity.GenScene;
import com.lnint.jess.generator.entity.GenScheme;
import com.lnint.jess.generator.entity.GenTemplate;
import com.lnint.jess.generator.utils.GenUtils;

/**
 * <P>代码生成方案管理Service</P>
 *
 * @version 2015-03-20
 * @author 位苏  2015-03-20 08:42:32
 * @since JDK 1.6
 */
@Service
@Transactional(readOnly = true)
public class GenSchemeService extends BaseService {

	@Autowired
	private GenSchemeDao genSchemeDao;
	@Autowired
	private GenSceneDao genSceneDao;
	
	public GenScheme get(String id) {
		return genSchemeDao.get(id);
	}
	
	public String generateCode(GenScheme genScheme){

		StringBuilder result = new StringBuilder();
		
		// 查询场景配置信息
		String id=genScheme.getGenScene().getId();
		GenScene genScene = genSceneDao.get(id);
		genScheme.setGenScene(genScene);
		
		// 获取所有代码模板
		GenConfig config = GenUtils.getConfig();
		
		// 获取模板列表
		List<GenTemplate> templateList = GenUtils.getTemplateList(config, genScheme.getGenScene().getSceneType(), false);
		List<GenTemplate> childTableTemplateList = GenUtils.getTemplateList(config, genScheme.getGenScene().getSceneType(), true);
		
		
		// 生成主表模板代码
		genScheme.setGenScene(genScene);
		Map<String, Object> model = GenUtils.getDataModel(genScheme);
		for (GenTemplate tpl : templateList){
			result.append(GenUtils.generateToFile(tpl, model, genScheme.getReplaceFile()));
		}
		
		if(childTableTemplateList.size()!=0){
			// 生成从表模板代码
			GenScheme genScheme1=new GenScheme();
			genScheme1=(GenScheme) StringUtils.depthClone(genScheme);
			Map<String, Object> subModel = GenUtils.getSubDataModel(genScheme1);
			for (GenTemplate tpl : childTableTemplateList){
				result.append(GenUtils.generateToFile(tpl, subModel, genScheme1.getReplaceFile()));
			}
		}		
		return result.toString();
	}
	
	public Page<GenScheme> find(Page<GenScheme> page, GenScheme genScheme) {

	    //HQL查询条件
		DetachedCriteria dc = genSchemeDao.createDetachedCriteria();
		if (StringUtils.isNotEmpty(genScheme.getName())){
			dc.add(Restrictions.like("name", "%"+genScheme.getName()+"%"));
		}
		dc.add(Restrictions.eq(GenScheme.FIELD_DEL_FLAG, GenScheme.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		//查询处理
		return genSchemeDao.find(page, dc);
	
	}
	
	@Transactional(readOnly = false)
	public void save(GenScheme genScheme) {
		genSchemeDao.clear();
		genSchemeDao.save(genScheme);
		
		// 生成代码
		if ("1".equals(genScheme.getFlag())){
			generateCode(genScheme);
		}
	}

	@Transactional(readOnly = false)
	public void delete(String id) {
		genSchemeDao.deleteById(id);
	}
}
